export function formatDate(stamp) {
  if (stamp) {
    const date = new Date(stamp);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  }
  return "--";
}

export const columns = [
  {
    title: "姓名",
    dataIndex: "empName",
    width: 120
  },
  {
    title: "身份证号",
    dataIndex: "empIdCard",
    width: 220
  },
  {
    title: "接单时间",
    dataIndex: "takeOrdersDate",
    width: 120,
    customRender: (text) => formatDate(text)
  },
  {
    title: "是否完结",
    dataIndex: "isFinish",
    width: 80,
    customRender: (text) => (text ? "是" : "否")
  }
];

export function deFormatValuesItem(item) {
  let res = item.content;
  if (item.fieldType === 9) {
    if (item.content) {
      res = item.content
        .split(",")
        .map((item) => {
          const arr = item.split("_");
          if (arr[0] === "d") {
            return arr[1];
          } else {
            return arr[1];
          }
        })
        .filter((item) => item)
        .join("-");
    }
  } else if ([6, 12].includes(item.fieldType)) {
    if (item.content) {
      res = item.content
        .split(",")
        .map((item) => {
          const arr = item.split("_");
          return arr[1];
        })
        .filter((item) => item)
        .join("-");
    }
  } else if ([3, 8].includes(item.fieldType)) {
    if (item.content) {
      res = item.content;
    }
  }
  return res || "--";
}

export function deFormatValues(list, fieldMap) {
  const data = {};
  for (let i = 0; i < list.length; i++) {
    const item = list[i];
    const key = `${item.id}_${item.fieldCode || ""}`;
    data[key] = item.content;
    if (fieldMap[key]) {
      if (fieldMap[key].type === "area_detail") {
        if (item.content) {
          data[key] = item.content
            .split(",")
            .map((item) => {
              const arr = item.split("_");
              if (arr[0] === "d") {
                data[`${key}_detail`] = arr[1];
                return false;
              } else {
                return parseInt(arr[0]);
              }
            })
            .filter((item) => item);
        }
      } else if (fieldMap[key].type === "area") {
        if (item.content) {
          data[key] = item.content
            .split(",")
            .map((item) => {
              const arr = item.split("_");
              return parseInt(arr[0]);
            })
            .filter((item) => item);
        }
      } else if (fieldMap[key].type === "date") {
        if (item.content) {
          if (item.content.includes("~")) {
            data[key] = item.content.split("~");
          } else {
            data[key] = item.content;
          }
        }
      }
    }
  }
  return data;
}

export function formatValues(res, fieldMap, areaSourceMap) {
  const list = [];
  const fields = Object.values(fieldMap);
  for (let i = 0; i < fields.length; i++) {
    const field = fields[i];
    const key = field.model;
    if (res[key]) {
      const keyArr = key.split("_");
      let value = res[key];
      if (field.type === "area_detail") {
        const areaArr = [];
        for (let j = 0; j < res[key].length; j++) {
          const aId = res[key][j];
          areaArr.push(`${aId}_${areaSourceMap[aId]["name"]}`);
        }
        if (res[`${key}_detail`]) {
          areaArr.push(`d_${res[`${key}_detail`]}`);
        }
        value = areaArr.join(",");
      } else if (field.type === "area") {
        const areaArr = [];
        for (let j = 0; j < res[key].length; j++) {
          const aId = res[key][j];
          areaArr.push(`${aId}_${areaSourceMap[aId]["name"]}`);
        }
        value = areaArr.join(",");
      } else if (field.type === "date") {
        if (Object.prototype.toString.call(value) === "[object Array]") {
          const dateArr = [];
          for (let j = 0; j < res[key].length; j++) {
            const date = res[key][j];
            dateArr.push(date.format("YYYY-MM-DD"));
          }
          value = dateArr.join("~");
        } else {
          value = res[key];
        }
      } else if (field.type === "upload") {
        if (Object.prototype.toString.call(value) === "[object Array]") {
          value = value.map((item) => item.url).join(",");
        }
      }
      list.push({
        fieldId: parseInt(keyArr[0]),
        fieldName: field.wrapper.label,
        id: field.wrapper.valueId,
        fieldCode: field.wrapper.fieldCode,
        content: value,
        subjectType: "dycl",
        subjectId: 0
      });
    } else {
      const keyArr = key.split("_");
      list.push({
        fieldId: parseInt(keyArr[0]),
        fieldName: field.wrapper.label,
        id: field.wrapper.valueId,
        fieldCode: field.wrapper.fieldCode,
        content: null,
        subjectType: "dycl",
        subjectId: 0
      });
    }
  }
  return list;
}
